﻿using Kimd.Common;
using Kimd.Logic;
using Kimd.Machine;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkStation.Common;

namespace WorkStation.Logic
{
    //前模组清料,后模组清料
    public class ClearBufferLogic : FlowBase
    {
        private int _moduleIndex;
        private PutOKNGLogic _putOKNGLogic;
        private Tray _recycleHeadTray1;
        private Tray _recycleHeadTray2;
        private Axis _ngAxis;
        public ClearBufferLogic(string flowName, FlowManager flowmanager, int moduleIndex) 
            : base(flowName, flowmanager)
        {
            this._moduleIndex = moduleIndex;
            if (moduleIndex == 0)
            {
                _putOKNGLogic = _flowManager.SelectFlow("前Arm模组") as PutOKNGLogic;
                _recycleHeadTray1 = (_flowManager.SelectFlow("Arm1模组") as TransferLogic).GetRecycleTray();
                _recycleHeadTray2 = (_flowManager.SelectFlow("Arm2模组") as TransferLogic).GetRecycleTray();
                _ngAxis = MachineResource.Instance.MachineAxis["精定位1NG轴"];
            }
            else
            {
                _putOKNGLogic = _flowManager.SelectFlow("后Arm模组") as PutOKNGLogic;
                _recycleHeadTray1 = (_flowManager.SelectFlow("Arm3模组") as TransferLogic).GetRecycleTray();
                _recycleHeadTray2 = (_flowManager.SelectFlow("Arm4模组") as TransferLogic).GetRecycleTray();
                _ngAxis = MachineResource.Instance.MachineAxis["精定位2NG轴"];
            }

            RegisterDirectSignal("清空Buffer完成");
        }

        public override void Calibrate()
        {
            base.Calibrate();
        }

        public override void Home()
        {
            base.Home();
        }
        public override void InSingleRunMode()
        {
            if (GlobalVariable.OnSingleRunPause != null)
                GlobalVariable.OnSingleRunPause();
        }
        public override void Init(out string errInfo)
        {
            base.Init(out errInfo);
        }

        public override void Method()
        {
            if (!CheckFlowCondition()) return;
            if (GlobalVariable.IsSpotChecking)
                return;

            WaitForSignal($"精定位移载{_moduleIndex + 1}", "清空Buffer");
            if (!CheckFlowCondition()) return;
            InSingleRunMode();
            if (_putOKNGLogic.GetBufferTray1().Helper.Any(c => c.Status == CellStatus.HasProduct))
            {
                if (!CheckFlowCondition()) return;
                MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"Buffer1有残余料", this.Description, 0, ""));
                for (int i = 0; i < 2; i++)
                {
                    if (_putOKNGLogic.GetBufferTray1().Helper.GetCell(i).Status == CellStatus.HasProduct)
                    {
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"取Buffer1的第{i+1}颗料", this.Description, 0, ""));
                        _putOKNGLogic.GetBuffer(_putOKNGLogic.GetBufferTray1(), "缓存矩阵",
                                                _moduleIndex == 0 ? GlobalVariable.BufferTray1UIFront : GlobalVariable.BufferTray1UIBack,
                                                0, $"吸嘴{3 + i}", i, _recycleHeadTray1);
                        if (!CheckFlowCondition()) return;
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"取Buffer1的第{i + 1}颗料完成", this.Description, 0, ""));
                    }
                }
            }
            if (_putOKNGLogic.GetBufferTray2().Helper.Any(c => c.Status == CellStatus.HasProduct))
            {
                if (!CheckFlowCondition()) return;
                MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"Buffer2有残余料", this.Description, 0, ""));
                for (int i = 0; i < 2; i++)
                {
                    if (_putOKNGLogic.GetBufferTray2().Helper.GetCell(i).Status == CellStatus.HasProduct)
                    {
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"取Buffer2的第{i + 1}颗料", this.Description, 0, ""));
                        _putOKNGLogic.GetBuffer(_putOKNGLogic.GetBufferTray2(), "缓存矩阵",
                                                _moduleIndex == 0 ? GlobalVariable.BufferTray2UIFront : GlobalVariable.BufferTray2UIBack,
                                                1, $"吸嘴{3 + i}", i, _recycleHeadTray2);
                        if (!CheckFlowCondition()) return;
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"取Buffer2的第{i + 1}颗料完成", this.Description, 0, ""));
                    }
                }
            }

            SingleAxisMove2Point(_ngAxis, "安全位");
            if (!CheckFlowCondition()) return;

            bool upReady = _moduleIndex == 0 ? (_flowManager.SelectFlow("精定位移载1") as PrecisionLogic).GetTwoLayerInPositionStatus() : (_flowManager.SelectFlow("精定位移载2") as PrecisionLogic).GetTwoLayerInPositionStatus();
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"当前上下层状态:{upReady}", this.Description, 0, ""));
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"开始放Buffer里的料至精定位", this.Description, 0, ""));
            _putOKNGLogic.PutOK(upReady, _recycleHeadTray1, _recycleHeadTray2);
            if(_moduleIndex == 0)
            {
                AxisMoveSimultaneously(new List<Axis>()
                {
                    MachineResource.Instance.MachineAxis["Arm1Z轴"],
                    MachineResource.Instance.MachineAxis["Arm2Z轴"],
                }, "待机位");
            }
            else
            {
                AxisMoveSimultaneously(new List<Axis>()
                {
                    MachineResource.Instance.MachineAxis["Arm3Z轴"],
                    MachineResource.Instance.MachineAxis["Arm4Z轴"],
                }, "待机位");
            }
            if (!CheckFlowCondition()) return;
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"放Buffer里的料至精定位完成", this.Description, 0, ""));

            SetSignal("清空Buffer完成");

        }

        public override void OnStop()
        {
            base.OnStop();
        }

        public override bool PreStartCheck()
        {
            return base.PreStartCheck();
        }
    }
}
